package com.xiaoyu.awakening.records.client.handler;


import com.xiaoyu.awakening.records.client.AwkRecordsClient;
import com.xiaoyu.awakening.records.client.annotation.AwkLogRecords;
import com.xiaoyu.awakening.records.client.collector.AbstractRecordsCollector;
import com.xiaoyu.awakening.records.dto.AwkOptLogsDTO;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

/**
 * AwkRecordsHandler
 *
 * @author wangxiaoyu
 * @date 2024/12/18 11:20
 * @since v1.0
 */
@Aspect
@Component
public class  AwkRecordsHandler {

    private AwkRecordsClient awkRecordsClient;
    private List<AbstractRecordsCollector> recordsCollectors;

    /**
     * awkRecordsPointCut
     *
     * @author wangxiaoyu
     * @date 2024/12/18 11:41
     * @since v1.0
     */
    @Pointcut("@annotation(com.xiaoyu.records.client.annotation.AwkLogRecords)")
    public void awkRecordsPointCut() {
    }

    @AfterReturning(value = "awkRecordsPointCut()", returning = "returnValue")
    public void awkRecordsAroundProcess(ProceedingJoinPoint point, Object returnValue) throws Throwable {
        point.proceed();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Method method = methodSignature.getMethod();
        Object[] args = point.getArgs();
        AwkLogRecords awkLogRecordsAnnotation = method.getAnnotation(AwkLogRecords.class);
        // todo: 做参数校验
        AbstractRecordsCollector collector = chooseCollector();
        // todo: collector 获取构建日志实体
        if (Objects.isNull(collector)) {
            return;
        }
        AwkOptLogsDTO awkOptRecordsDTO = collector.collect();
        awkRecordsClient.publishRecords(awkOptRecordsDTO);
    }

    private AbstractRecordsCollector chooseCollector() {
        return null;
    }

}
